package com.ambulance.project.biz.controller.pad;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ambulance.framework.aspectj.lang.annotation.Log;
import com.ambulance.framework.aspectj.lang.enums.BusinessType;
import com.ambulance.framework.aspectj.lang.enums.OperatorType;
import com.ambulance.framework.web.domain.AjaxResult;
import com.ambulance.framework.web.domain.AjaxResultData;
import com.ambulance.project.biz.entity.Dispatch;
import com.ambulance.project.biz.entity.DispatchCase;
import com.ambulance.project.biz.entity.GCSAnswer;
import com.ambulance.project.biz.entity.Gcs;
import com.ambulance.project.biz.service.DispatchCaseService;
import com.ambulance.project.biz.service.GCSAnswerService;
import com.ambulance.project.biz.service.GcsService;
import com.ambulance.project.manager.utils.DoctorAndNurseCommonUtil;
import com.ambulance.utils.ChildUtils;
import com.ambulance.utils.CommonUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.google.common.collect.Maps;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Api(tags = {"随车医生-GCS答案相关接口"})
@RequestMapping(value = "/pad/v1/GCS/answer", method = RequestMethod.POST)
@Controller
public class GCSAnswerController {

    @Autowired
    private GCSAnswerService gcsAnswerService;
    @Autowired
    private GcsService gcsService;
    @Autowired
    private DoctorAndNurseCommonUtil doctorAndNurseCommonUtil;
    @Autowired
    private DispatchCaseService dispatchCaseService;

    @RequestMapping(value = "/add")
    @ResponseBody
    @ApiOperation(value = "保存GCS答案", notes = "保存GCS答案", produces = "application/json")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "content", value = "答案json串 {key(题干的id):value(答案的id)", required = true, paramType = "form", dataType = "String"),
    })
    @ApiResponses({
            @ApiResponse(code = 200, response = AjaxResultData.class, message = "响应")
    })
    @Log(title = "填写gcs答案", businessType = BusinessType.OTHER, operatorType = OperatorType.MOBILE)
    public Object add(
            @RequestParam(value = "content") String content
    ) {
        final Dispatch dispatchInfo = doctorAndNurseCommonUtil.getDispatch();
        final DispatchCase dispatchCaseServiceOne = dispatchCaseService.getOne(new QueryWrapper<DispatchCase>().eq("dispatchID", dispatchInfo.getDispatchID()));
        final GCSAnswer dcid = gcsAnswerService.getOne(new QueryWrapper<GCSAnswer>().eq("dcid", dispatchCaseServiceOne.getDispatchCaseid()));
        if (!CommonUtils.checkNull(dcid)) {
            gcsAnswerService.removeById(dcid.getId());
//            throw new CustomException("不可重复提交GCS评分");
        }
        Map maps = JSON.parseObject(content, Map.class);
        int sum = 0;
        for (Object obj : maps.keySet()) {
            final Gcs gcsServiceOne = gcsService.getOne(new QueryWrapper<Gcs>().eq("id", Integer.parseInt(maps.get(obj).toString())));
            if (!CommonUtils.checkNull(gcsServiceOne)) {
                sum += gcsServiceOne.getNumber();
            }
        }
        final GCSAnswer gcsAnswer = new GCSAnswer();
        gcsAnswer.setContent(content);
        gcsAnswer.setDcid(dispatchCaseServiceOne.getDispatchCaseid());
        gcsAnswer.setSum(sum);
        gcsAnswerService.save(gcsAnswer);
        HashMap<String, Object> map = Maps.newHashMap();
        map.put("sum", sum);
        return AjaxResult.success(map);
    }

    // 核实后不需要进行修改操作
//    @RequestMapping(value = "/update")
//    @ResponseBody
//    @ApiOperation(value = "更新GCS答案", notes = "更新GCS答案", produces = "application/json")
//    @ApiImplicitParams({
//            @ApiImplicitParam(name = "content", value = "答案json串", required = true, paramType = "form", dataType = "String"),
//            @ApiImplicitParam(name = "dcid", value = "病历id", required = true, paramType = "form", dataType = "int"),
//    })
//    @ApiResponses({
//            @ApiResponse(code = 200, response = AjaxResultData.class, message = "响应")
//    })
//    public Object update(
//            @RequestParam(value = "content") String content,
//            @RequestParam(value = "dcid") Integer dcid
//    ) {
//        Map maps = JSON.parseObject(content, Map.class);
//        int sum = 0;
//        for (Object obj : maps.keySet()) {
//            sum += Integer.parseInt(maps.get(obj).toString());
//        }
//        final GCSAnswer gcsAnswer = new GCSAnswer();
//        gcsAnswer.setContent(content);
//        gcsAnswer.setDcid(dcid);
//        gcsAnswer.setSum(sum);
//        final boolean save = gcsAnswerService.update(gcsAnswer,new QueryWrapper<GCSAnswer>().eq("dcid",dcid));
//        return AjaxResult.AutoResponse(save);
//    }


    @ApiOperation(value = "获取GCS答案", notes = "获取GCS答案", produces = "application/json")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "dispatchId", value = "调度id", required = true, paramType = "form", dataType = "int"),
    })
    @ApiResponses({
            @ApiResponse(code = 200, response = AjaxResultData.class, message = "响应")
    })
    @RequestMapping(value = "/getAnswer")
    @ResponseBody
    public Object getAnswer(@RequestParam(value = "dispatchId") Integer dispatchId) {
        final DispatchCase dispatchID = dispatchCaseService.getOne(new QueryWrapper<DispatchCase>().eq("dispatchID", dispatchId));
        Integer dcid = dispatchID.getDispatchCaseid();
        final QueryWrapper<GCSAnswer> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("dcid", dcid);
        final GCSAnswer one = gcsAnswerService.getOne(queryWrapper);
        if (CommonUtils.checkNull(one)) {
//            throw new CustomException("暂无gcs信息");
            final List<Gcs> gcsList = gcsService.list();
            return AjaxResult.success(ChildUtils.getGCSChild(gcsList));
        }
        final JSONObject object = JSON.parseObject(one.getContent());
        final List<Gcs> gcsList = ChildUtils.getGCSChild(gcsService.list());
        for (Gcs gcs : gcsList) {
            gcs.setAnswer(object.getInteger(String.valueOf(gcs.getId())));
        }
        return AjaxResult.success(gcsList);
    }


}
